﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;

namespace B13_Ex03_David_300512340_Alexey_321203689
{
    public class AlbumWrapper
    {
        public Album AlbumInformation { get; set; }

        public override string ToString()
        {
            return string.Format("{0} ({1})", AlbumInformation.Name, AlbumInformation.UpdateTime);
        }

        public string GetAlbumCoverURL()
        {
            string albumCoverURL = AlbumInformation.CoverPhotoThumbURL != null ?
                AlbumInformation.CoverPhotoThumbURL : AlbumInformation.PictureThumbURL;

            return albumCoverURL;
        }

        public List<PhotoWrapper> GetAlbumPhotosList()
        {
            List<PhotoWrapper> albumPhotosList = new List<PhotoWrapper>();

            if (AlbumInformation != null)
            {
                try
                {
                    foreach (Photo photo in AlbumInformation.Photos)
                    {
                        albumPhotosList.Add(new PhotoWrapper() { PhotoInformation = photo });
                    }
                }
                catch (Exception e)
                {
                    string msg = string.Format("Error fetching album photos.!");
                    throw new Exception(msg, e);
                }
            }

            return albumPhotosList;
        }

        public void SaveAlbum(AlbumWrapper i_Album, string i_PathSelected, string i_PhotoName)
        {
            List<PhotoWrapper> albumPhotosList = i_Album.GetAlbumPhotosList();
            int imageCounter = 1;
            string photoName = i_PhotoName.Length == 0 ? "facebookImage" : i_PhotoName;
            string fullPhotoName = albumPhotosList.Count == 1 ?
                photoName : string.Format("{0}{1}", photoName, imageCounter);
            string path = albumPhotosList.Count == 1 ?
                string.Format("{0}\\{1}.jpg", i_PathSelected, photoName) : string.Format("{0}\\{1}{2}.jpg", i_PathSelected, photoName, imageCounter);

            if (AlbumInformation != null)
            {
                try
                {
                    MessageBox.Show(null, "Saving photos, please wait.", "Save Photos", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    foreach (PhotoWrapper photo in albumPhotosList)
                    {
                        photo.SavePhoto(photo, i_PathSelected, fullPhotoName);
                        imageCounter++;
                        path = string.Format("{0}\\{1}{2}.jpg", i_PathSelected, photoName, imageCounter);
                        fullPhotoName = string.Format("{0}{1}", photoName, imageCounter);
                    }

                    MessageBox.Show(null, "Done saving photos.", "Save Photos", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception e)
                {
                    string msg = string.Format("Error, can not save photos!");
                    throw new Exception(msg, e);
                }
            }
        }
    }
}
